package com.jt.sys.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.druid.util.StringUtils;
import com.jt.common.annotation.RequestLog;
import com.jt.common.exception.ServiceException;
import com.jt.common.vo.PageObject;
import com.jt.sys.dao.SysRoleDao;
import com.jt.sys.dao.SysRoleMenuDao;
import com.jt.sys.dao.SysUserRoleDao;
import com.jt.sys.entity.SysRole;
import com.jt.sys.service.SysRoleService;
@Service
@Transactional
public class SysRoleServiceImpl implements SysRoleService {
	@Autowired
	private SysRoleDao sysRoleDao;
	@Autowired
	private SysRoleMenuDao sysRoleMenuDao;
	@Autowired
	private SysUserRoleDao sysUserRoleDao;
	@Override
	@RequestLog("查看角色信息")
	public PageObject<SysRole> findPageObjects(String name, Integer pageCurrent) {
		if(pageCurrent==null|pageCurrent<1)
			throw new IllegalArgumentException("输入的页码不正确");
		int rowCount = sysRoleDao.getRowCount(name);
		if(rowCount==0)
			throw new ServiceException("没查询到该条记录");
		int pageSize=6;
		int startIndex=(pageCurrent-1)*pageSize;
		List<SysRole> records = sysRoleDao.findPageObjects(name, startIndex, pageSize);
		PageObject<SysRole> pageObject=new PageObject<>();
		pageObject.setPageCurrent(pageCurrent);
		pageObject.setPageSize(pageSize);
		pageObject.setRecords(records);
		pageObject.setRowCount(rowCount);
		return pageObject;
	}
	
	@Override
	@RequestLog("删除角色信息")
	public int deleteObject(Integer id) {
		if(id==null||id<1)
			throw new ServiceException("id的值不正确");
		int rows = sysRoleDao.deleteObject(id);
		if(rows==0)
			throw new ServiceException("该数据可能已经不存在");
		sysRoleMenuDao.deleteObjectsByRoleId(id);
		sysUserRoleDao.deleteObjectsByRoleId(id);
		return rows;
	}

	@Override
	@RequestLog("添加角色信息")
	public int saveObject(SysRole entity, Integer[] menuIds) {
		if(entity==null)
			throw new ServiceException("保存数据不能为空");
		if(StringUtils.isEmpty(entity.getName()))
			throw new ServiceException("角色名不能为空");
		if(menuIds==null||menuIds.length==0)
			throw new ServiceException("必须赋予权限");
		entity.setCreatedTime(new Date());
		entity.setModifiedTime(new Date());
		entity.setCreatedUser("admin");
		entity.setModifiedUser("admin");
		int rows = sysRoleDao.insertObject(entity);
		sysRoleMenuDao.insertObject(entity.getId(), menuIds);
		if (rows==0)
			throw new ServiceException("添加失败");
		
		return rows;
		
	}

	@Override
	public Map<String, Object> findObjectById(Integer id) {
		if(id==null||id<1){
			throw new IllegalArgumentException("参数id不合法");
		}
		SysRole sysRole = sysRoleDao.findObjectById(id);
		if(sysRole==null)
			throw new ServiceException("此对象可能不存在了");
		List<Integer> menuIds = sysRoleMenuDao.findMenuIdsByRoleId(id);
		Map<String, Object> map=new HashMap<>();
		map.put("sysRole", sysRole);
		map.put("menuIds", menuIds);
		return map;
	}

	@Override
	@RequestLog("修改角色信息")
	public int updateObject(SysRole entity, Integer[] menuIds) {
		if(entity==null)
			throw new ServiceException("更新对象不能为空");
		if(entity.getId()==null)
	    	throw new ServiceException("id的值不能为空");
	    if(StringUtils.isEmpty(entity.getName()))
	    	throw new ServiceException("角色名不能为空");
	    if(menuIds==null||menuIds.length==0)
	    	throw new ServiceException("必须为角色指定一个权限");
	    entity.setModifiedTime(new Date());
	    entity.setModifiedUser("admin");
	    int rows = sysRoleDao.updateObject(entity);
	    if(rows==0)
	    	throw new ServiceException("此记录可能已不存在了");
	    sysRoleMenuDao.deleteObjectsByRoleId(entity.getId());
	    sysRoleMenuDao.insertObject(entity.getId(), menuIds);
		return rows;
	}

}
